#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include "Splines.h"

using namespace std;

double f(double x) {
    return 1.0 / (1 + 25 * x * x);
}

double df1(double x) {
    return -50 * x / ((1 + 25 * x * x) * (1 + 25 * x * x));
}

double df2(double x) {
    return 50 * (75 * x * x - 1) / ((1 + 25 * x * x) * (1 + 25 * x * x) * (1 + 25 * x * x));
}

int main(int argc, char* argv[]) {
    vector<int> ns = {5, 10, 20, 40, 80};
    vector<ppForm> ppForms;

    for (int n : ns) {
        vector<double> xValues, yValues;
        for (int i = 0; i <= n; i++) {
            double x = -1.0 + 2.0 * i / n;
            xValues.push_back(x);
            yValues.push_back(f(x));
        }
        ppForms.push_back(ppForm(xValues, yValues));
    }

    ofstream A("QA.txt");

    for (size_t i = 0; i < ns.size(); i++) {
        int n = ns[i];
        ppForm& spline = ppForms[i];

        A << "n=" << n + 1 << endl;

        A << "线性插值：" << endl;
        for (double xx = -1; xx <= 1; xx += 0.001) {
            A << spline.S1(xx) << " ";
        }
        A << endl;

        A << "完全三次样条：" << endl;
        for (double xx = -1; xx <= 1; xx += 0.001) {
            A << spline.S3(1, df1(-1), df1(1), xx) << " ";
        }
        A << endl;

        A << "D2三次样条：" << endl;
        for (double xx = -1; xx <= 1; xx += 0.001) {
            A << spline.S3(2, df2(-1), df2(1), xx) << " ";
        }
        A << endl;

        A << "自然三次样条：" << endl;
        for (double xx = -1; xx <= 1; xx += 0.001) {
            A << spline.S3(3, 0, 0, xx) << " ";
        }
        A << endl;
    }

    A.close();

    cout << "Interpolations saved to QA.txt" << endl;
    return 0;
}
